home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / Tool Chest / Development Platforms / AppsToGo / AppsToGo.src / DTS.Draw / TRRectObj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-18  |  7.1 KB  |  272 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        TRRectObj.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1992-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19. /* See the files "=How to write your app" and "=Using TreeObj.c" for information
  20. ** on this function. */
  21.  
  22. /* This file implements the messages for the round-rect object.  Many of the messages
  23. ** can be handled by the rect object, as they deal with a rect structure.  Only
  24. ** a few of them are round-rect-specific. */
  25.  
  26. /* It would seem that you would want a custom hit-test message handler here, but
  27. ** the rect object first checks to see if the hit is within the bounding box of
  28. ** the object, and if so, it then calls the object to return the region.  This
  29. ** allows the rect object to generically handle hit-testing. */
  30.  
  31.  
  32.  
  33. /*****************************************************************************/
  34.  
  35.  
  36.  
  37. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  38. #include "App.protos.h"        /* Get the prototypes for the application.        */
  39.  
  40. #ifndef __OSEVENTS__
  41. #include <OSEvents.h>
  42. #endif
  43.  
  44. #ifndef __OSUTILS__
  45. #include <OSUtils.h>
  46. #endif
  47.  
  48. #ifndef __QUICKDRAW__
  49. #include <Quickdraw.h>
  50. #endif
  51.  
  52. #ifndef __STRING__
  53. #include <String.h>
  54. #endif
  55.  
  56. #ifndef __TREEOBJ2__
  57. #include "TreeObj2.h"
  58. #endif
  59.  
  60. #ifndef __UTILITIES__
  61. #include "Utilities.h"
  62. #endif
  63.  
  64.  
  65.  
  66. #pragma segment DrawObjects
  67. long    TRRectObj(TreeObjHndl hndl, short message, long data)
  68. {
  69.     Rect        rct, grabber;
  70.     RgnHandle    rgn;
  71.     short        w, h, oldh, oldw;
  72.     ClickInfo    *click;
  73.     TreeObjHndl    hhndl;
  74.     Point        where, pt, curMouse;
  75.     EventRecord    option;
  76.     RGBColor    rgb, rgb2;
  77. #if VH_VERSION
  78.     char        *cptr;
  79. #endif
  80.  
  81.     switch (message) {
  82.         case INITMESSAGE:
  83.             TRectObj(hndl, message, data);
  84.             mDerefRRect(hndl)->width = mDerefRRect(hndl)->height = 20;
  85.             break;
  86.  
  87.         case FREEMESSAGE:
  88.         case COPYMESSAGE:
  89.         case UNDOMESSAGE:
  90.         case CONVERTMESSAGE:
  91.         case FREADMESSAGE:
  92.         case FWRITEMESSAGE:
  93.         case HREADMESSAGE:
  94.         case HWRITEMESSAGE:
  95.         case GETOBJRECTMESSAGE:
  96.         case SETOBJRECTMESSAGE:
  97.         case SECTOBJRECTMESSAGE:
  98.         case GETBBOXMESSAGE:
  99.         case CLICKMESSAGE:
  100.         case KEYMESSAGE:
  101.         case SETSELECTMESSAGE:
  102.         case GETSELECTMESSAGE:
  103.         case COMPAREMESSAGE:
  104.             return(TRectObj(hndl, message, data));
  105.             break;
  106.  
  107.         case HITTESTMESSAGE:
  108.             click = (ClickInfo *)data;
  109.             hhndl = (TreeObjHndl)TRectObj(hndl, message, data);
  110.                 /* We must call TRectObj::HITTESTMESSAGE to set some static
  111.                 ** variables that TRectObj::SELECTOBJMESSAGE uses. */
  112.             if ((!hhndl) && (click->message == HITTESTGRABBER)) {
  113.                 if (mDerefRoot(GetRootHndl(hndl))->numSelected == 1) {
  114.                     if (mDerefRRect(hndl)->selected) {
  115.                         rct   = mDerefRRect(hndl)->rect;
  116.                         where = click->localEvent.where;
  117.                         if (PtInRect(where, &rct)) {
  118.                             GetMouse(&curMouse);
  119.                             grabber.top  = pt.v = (rct.top  + (mDerefRRect(hndl)->height / 2));
  120.                             grabber.left = pt.h = (rct.left + (mDerefRRect(hndl)->width / 2));
  121.                             grabber.bottom = (grabber.top  -= 3) + 6;
  122.                             grabber.right  = (grabber.left -= 3) + 6;
  123.                             if (PtInRect(where, &grabber)) {
  124.                                 click->localEvent.where = pt;
  125.                                 click->offset.h         = pt.h - curMouse.h;
  126.                                 click->offset.v         = pt.v - curMouse.v;
  127.                                 click->oldFlip          = 0;
  128.                                 click->newFlip          = 0;
  129.                                 click->grabber          = 7;
  130.                                 hhndl = hndl;
  131.                             }
  132.                         }
  133.                     }
  134.                 }
  135.             }
  136.             return((long)hhndl);
  137.             break;
  138.  
  139.         case GETRGNMESSAGE:
  140.             rgn = NewRgn();
  141.             OpenRgn();
  142.             rct = mDerefRRect(hndl)->rect;
  143.             FrameRoundRect(&rct, mDerefRRect(hndl)->width, mDerefRRect(hndl)->height);
  144.             CloseRgn(rgn);
  145.             return((long)rgn);
  146.             break;
  147.  
  148.         case DRAWMESSAGE:
  149.             rct = mDerefRRect(hndl)->rect;
  150.             h   = mDerefCommon(hndl)->penHeight;
  151.             w   = mDerefCommon(hndl)->penWidth;
  152.             PenSize(w, h);
  153.             w = mDerefRRect(hndl)->width;
  154.             h = mDerefRRect(hndl)->height;
  155.             switch (data) {
  156.                 case DRAWOBJ:
  157.                     if (gQDVersion)
  158.                         GetForeColor(&rgb);
  159.                     ForeColor(whiteColor);
  160.                     if (gQDVersion) {
  161.                         rgb2 = mDerefLine(hndl)->contentColor;
  162.                         RGBForeColor(&rgb2);
  163.                     }
  164.                     PaintRoundRect(&rct, w, h);
  165.                     ForeColor(blackColor);
  166.                     if (gQDVersion) {
  167.                         rgb2 = mDerefLine(hndl)->borderColor;
  168.                         RGBForeColor(&rgb2);
  169.                     }
  170.                     FrameRoundRect(&rct, w, h);
  171.                     if (gQDVersion)
  172.                         RGBForeColor(&rgb);
  173.                     break;
  174.                 case ERASEOBJ:
  175.                     EraseRoundRect(&rct, w, h);
  176.                     break;
  177.                 case DRAWSELECT:
  178.                     TRectObj(hndl, message, data);
  179.                     if (mDerefCommon(hndl)->selected) {
  180.                         grabber.top  = rct.top  + (h / 2);
  181.                         grabber.left = rct.left + (w / 2);
  182.                         grabber.bottom = (grabber.top  -= 3) + 6;
  183.                         grabber.right  = (grabber.left -= 3) + 6;
  184.                         InvertRect(&grabber);
  185.                     }
  186.                     break;
  187.                 case DRAWGHOST:
  188.                     PenMode(patXor);
  189.                     FrameRoundRect(&rct, w, h);
  190.                     break;
  191.                 case DRAWMASK:
  192.                     FillRoundRect(&rct, w, h, (ConstPatternParam)&qd.black);
  193.                     break;
  194.             }
  195.             PenNormal();
  196.             break;
  197.  
  198.         case PRINTMESSAGE:
  199.             TRRectObj(hndl, DRAWMESSAGE, DRAWOBJ);
  200.             break;
  201.  
  202. #if VH_VERSION
  203.         case VHMESSAGE:
  204.             cptr = ((VHFormatDataPtr)data)->data;
  205.             ccatchr(cptr, 13, 2);
  206.             ccat   (cptr, "$10: TRRectObj:");
  207.             ccatchr(cptr, 13, 1);
  208.             ccat   (cptr, "  $00: selected = ");
  209.             ccatdec(cptr, mDerefRRect(hndl)->selected);
  210.             ccatchr(cptr, 13, 1);
  211.             rct = mDerefRRect(hndl)->rect;
  212.             ccat   (cptr, "  $02: rect     = ($");
  213.             ccathex(cptr, 0, 4, 4, rct.top);
  214.             ccat   (cptr, ",$");
  215.             ccathex(cptr, 0, 4, 4, rct.left);
  216.             ccat   (cptr, ",$");
  217.             ccathex(cptr, 0, 4, 4, rct.bottom);
  218.             ccat   (cptr, ",$");
  219.             ccathex(cptr, 0, 4, 4, rct.right);
  220.             ccat   (cptr, ")");
  221.             ccatchr(cptr, 13, 1);
  222.             ccat   (cptr, "  $0A: width    = ");
  223.             ccatdec(cptr, mDerefRRect(hndl)->width);
  224.             ccatchr(cptr, 13, 1);
  225.             ccat   (cptr, "  $0C: height   = ");
  226.             ccatdec(cptr, mDerefRRect(hndl)->height);
  227.             return(true);
  228.             break;
  229. #endif
  230.  
  231.         case SIZEMESSAGE:
  232.             click = (ClickInfo *)data;
  233.             if (click->grabber != 7)
  234.                 return(TRectObj(hndl, message, data));
  235.  
  236.             DoTreeObjMethod(hndl, GETOBJRECTMESSAGE, (long)&rct);
  237.             oldw = mDerefRRect(hndl)->width;
  238.             oldh = mDerefRRect(hndl)->height;
  239.  
  240.             GetMouse(&curMouse);
  241.             curMouse.h += click->offset.h;
  242.             curMouse.v += click->offset.v;
  243.             w = (curMouse.h - rct.left) * 2;
  244.             h = (curMouse.v - rct.top) * 2;
  245.             if (w < 10) w = 10;
  246.             if (h < 10) h = 10;
  247.             if (w > (rct.right  - rct.left)) w = rct.right  - rct.left;
  248.             if (h > (rct.bottom - rct.top))  h = rct.bottom - rct.top;
  249.  
  250.             OSEventAvail(nullEvent, &option);
  251.             if (option.modifiers & shiftKey) {
  252.                 if (w > h) w = h;
  253.                 if (h > w) h = w;
  254.             }
  255.             if ((w != oldw) || (h != oldh)) {
  256.                 mDerefRRect(hndl)->width  = w;
  257.                 mDerefRRect(hndl)->height = h;
  258.                 return(true);
  259.             }
  260.             return(false);
  261.             break;
  262.  
  263.         default:
  264.             break;
  265.     }
  266.  
  267.     return(noErr);
  268. }
  269.  
  270.  
  271.  
  272.